export default abstract class Storage {
    static getItem(key: string, defaultValue = ''): string {
        const _value = localStorage.getItem(key)
        if (undefined === _value || null === _value) {
            return defaultValue
        }
        return _value
    }

    static setItem(key: string, value: string | number): void {
        if (undefined === value || null === value) {
            localStorage.removeItem(key)
        } else {
            localStorage.setItem(key, value + '')
        }
    }

    static getNumber(key: string, defaultValue = 0): number {
        const _value = localStorage.getItem(key)
        if (undefined === _value || null === _value) {
            return defaultValue
        }
        const temp = Number(_value)
        return isNaN(temp) ? defaultValue : temp
    }

    static getObject(key: string, defaultValue: object): object | null | undefined {
        const _value = localStorage.getItem(key)

        if (undefined === _value || null === _value) {
            return defaultValue
        }
        try {
            return JSON.parse(_value)
        } catch (e) {
            return defaultValue
        }
    }

    static getBoolean(key: string, defaultValue = false): boolean {
        const _value = localStorage.getItem(key)

        if (undefined === _value || null === _value) {
            return defaultValue
        }
        return _value === 'true'
    }

    static setObject(key: string, value: object): void {
        try {
            const temp = JSON.stringify(value)
            localStorage.setItem(key, temp)
        } catch (e) {
            localStorage.removeItem(key)
        }
    }

    static setBoolean(key: string, value: boolean): void {
        const temp = value ? 'true' : 'false';
        localStorage.setItem(key, temp)
    }

    static removeItem(key: string): void {
        localStorage.removeItem(key)
    }
}
